import { defineStore } from 'pinia'

export const useDishStore = defineStore('dish', {
  state: () => ({
    dishes: [],
    categories: [
      { id: 1, name: '全部', icon: 'grid' },
      { id: 2, name: '早餐', icon: 'sun-rise' },
      { id: 3, name: '晚餐', icon: 'moon' },
      { id: 4, name: '我的菜品', icon: 'edit-pen' }
    ],
    
    // 添加菜品功能
    customDishes: [],
    currentCategory: 1,
    searchKeyword: '',
    loading: false,
    banners: []
  }),
  
  getters: {
    allDishes: (state) => state.dishes,
    filteredDishes: (state) => {
      let filtered = state.dishes
      
      // 按分类筛选
      if (state.currentCategory !== 1) {
        filtered = filtered.filter(dish => dish.categoryId === state.currentCategory)
      }
      
      // 按关键词搜索
      if (state.searchKeyword) {
        filtered = filtered.filter(dish => 
          dish.name.includes(state.searchKeyword) || 
          dish.description.includes(state.searchKeyword)
        )
      }
      
      return filtered
    },
    recommendDishes: (state) => state.dishes.filter(dish => dish.isRecommend),
    hotDishes: (state) => state.dishes.filter(dish => dish.isHot)
  },
  
  actions: {
    async getDishes() {
      this.loading = true
      try {
        // 默认菜品数据 - 早餐和晚餐
        const defaultDishes = [
          {
            id: 1,
            name: '爱心煎蛋吐司',
            description: '爱心造型的煎蛋吐司，营养早餐首选',
            points: 20,
            image: '/static/images/dish1.png',
            category: 'breakfast',
            categoryId: 2,
            isRecommend: true,
            isHot: false,
            tags: ['早餐', '简单'],
            ingredients: ['吐司', '鸡蛋', '黄油', '番茄酱'],
            nutrition: '热量: 280大卡, 蛋白质: 12g, 碳水: 35g'
          },
          {
            id: 2,
            name: '甜蜜燕麦粥',
            description: '暖心燕麦粥，搭配蜂蜜和水果',
            points: 15,
            image: '/static/images/dish2.png',
            category: 'breakfast',
            categoryId: 2,
            isRecommend: true,
            isHot: false,
            tags: ['早餐', '健康'],
            ingredients: ['燕麦', '牛奶', '蜂蜜', '蓝莓'],
            nutrition: '热量: 220大卡, 纤维: 8g, 蛋白质: 8g'
          },
          {
            id: 3,
            name: '浪漫烛光牛排',
            description: '精选牛排，浪漫晚餐必备',
            points: 50,
            image: '/static/images/dish3.png',
            category: 'dinner',
            categoryId: 3,
            isRecommend: true,
            isHot: true,
            tags: ['晚餐', '浪漫'],
            ingredients: ['牛排', '黑胡椒', '迷迭香', '红酒'],
            nutrition: '热量: 450大卡, 蛋白质: 35g, 脂肪: 28g'
          },
          {
            id: 4,
            name: '双人海鲜意面',
            description: '新鲜海鲜配意面，浪漫晚餐',
            points: 45,
            image: '/static/images/dish4.png',
            category: 'dinner',
            categoryId: 3,
            isRecommend: false,
            isHot: true,
            tags: ['晚餐', '海鲜'],
            ingredients: ['意面', '虾', '蛤蜊', '番茄'],
            nutrition: '热量: 380大卡, 蛋白质: 25g, 碳水: 45g'
          },
          {
            id: 5,
            name: '爱心水果沙拉',
            description: '新鲜水果拼盘，健康轻食',
            points: 25,
            image: '/static/images/dish5.png',
            category: 'breakfast',
            categoryId: 2,
            isRecommend: false,
            isHot: false,
            tags: ['早餐', '健康'],
            ingredients: ['草莓', '蓝莓', '香蕉', '酸奶'],
            nutrition: '热量: 150大卡, 维生素C: 60mg'
          },
          {
            id: 6,
            name: '浓情巧克力蛋糕',
            description: '丝滑巧克力蛋糕，甜蜜分享',
            points: 35,
            image: '/static/images/dish6.png',
            category: 'dinner',
            categoryId: 3,
            isRecommend: true,
            isHot: false,
            tags: ['晚餐', '甜点'],
            ingredients: ['巧克力', '奶油', '鸡蛋', '糖'],
            nutrition: '热量: 320大卡, 碳水: 40g, 脂肪: 18g'
          }
        ]
        
        // 合并自定义菜品
        this.dishes = [...defaultDishes, ...this.customDishes]
        
        this.banners = [
          {
            id: 1,
            image: '/static/images/banner1.png',
            title: '今日特餐：浪漫烛光晚餐',
            dishId: 1
          },
          {
            id: 2,
            image: '/static/images/banner2.png',
            title: '甜蜜双人套餐限时优惠',
            dishId: 2
          }
        ]
        
        this.loading = false
        return { success: true, data: this.dishes }
      } catch (error) {
        this.loading = false
        return { success: false, message: error.message }
      }
    },
    
    setCurrentCategory(categoryId) {
      this.currentCategory = categoryId
    },
    
    setSearchKeyword(keyword) {
      this.searchKeyword = keyword
    },
    
    getDishById(dishId) {
      return this.dishes.find(dish => dish.id === dishId)
    }
  }
})